package org.example.server8001.config;

import org.example.server8001.controller.RefController;
import org.example.SystemDemo.comment.enity.HostInfo;
import org.example.server8001.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;

/**
 * @author world
 * @desc 自动下线
 */
@Configuration
@EnableScheduling
public class ScheduledOffline {

    @Autowired
    private InfoService infoService;

    /**
     * 自动下线未发送心跳包的设备 ,间隔 10s
     */
    @Scheduled(fixedRate = 10000)
    public void automaticOffline(){
        ArrayList<Long> ids = new ArrayList<>();

        // 获取服务器列表
        final List<HostInfo> list = infoService.list();

        for (HostInfo info : list){
            // 如果id不存在存活列表中
            if (!RefController.ids.contains(info.getId())){
                info.setConnectStatus(false);
                infoService.updateById(info);
            }
        }

        // 重新刷新存活列表
        RefController.ids = new ArrayList<>();
    }
}
